package com.剑指offer;

import java.util.ArrayList;

public class _42和为S的两个数字
{
    public static void main(String[] args)
    {
        int[] a = {1,2,4,7,11,15};
        FindNumbersWithSum(a, 15);
    }

    public static ArrayList<Integer> FindNumbersWithSum(int[] array, int sum)
    {
        int length = array.length;
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<ArrayList<Integer>> bList = new ArrayList<>();
        if (length == 0 || length == 1)
        {
            return list;
        }
        int temp1 = 0;
        int temp2 = 0;

        while (true)
        {
            while (true)
            {
                if (array[temp1] + array[temp2] > sum || temp2 >= length - 1)
                {
                    break;
                }
                temp2++;
                if (array[temp1] + array[temp2] == sum)
                {
                    list.add(array[temp1]);
                    list.add(array[temp2]);
                    bList.add(new ArrayList<>(list));
                    break;
                }
            }
            temp1++;
            temp2 = temp1;
            list.clear();
            if (array[temp1] > sum / 2 || temp1 >= length - 1)
            {
                break;
            }
        }
        System.out.println(bList);
        int size = bList.size();
        if(size == 0){
            return list;
        }
        ArrayList<Integer>  min = new ArrayList<>();
        min = bList.get(0);
        for (int i = 0; i < size; i++)
        {
            min = compare(min,bList.get(i));
        }
        System.out.println(min);
        return min;
    }

    public static ArrayList<Integer> compare(ArrayList<Integer> list1, ArrayList<Integer> list2)
    {
        int s1 = list1.get(0) * list1.get(1);
        int s2 = list2.get(0) * list2.get(1);
        return (s1 > s2) ? list2 : list1;

    }

}
